package
{
import com.chcplayer.ChcPlayerApp;
import com.chcplayer.ChcPlayerAppModel;
import com.chcplayer.FileListModel;
import com.chcplayer.ui.AboutWindow;
import com.chcplayer.ui.ChooseBgColorWindow;
import com.chcplayer.ui.SetBgColorWindow;
import com.chcplayer.ui.SetBgRectExWindow;
import com.chcplayer.ui.SetScalingWindow;

import flash.desktop.NativeApplication;
import flash.display.NativeMenu;
import flash.display.NativeMenuItem;
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.events.InvokeEvent;
import flash.filesystem.File;
import flash.net.FileFilter;
import flash.ui.Keyboard;

/**
 * author yh
 */
[SWF(width="1024", height="768", frameRate="30", backgroundColor="0xb0b0b0", scriptTimeLimit="90000000")]
public class CHCPlayer extends Sprite
{
	/**
	 */
	public static const APP_NAME:String = "CHCPlayer";
	/**
	 */
	public static const ARR_PIC_FILTER:Array = [new FileFilter("图象文件(*.jpg;*.png)", "*.jpg;*.png")];
	/**
	 */
	public static const ARR_CHC_FILTER:Array = [new FileFilter("高清分层图象文件(*.chc)", "*.chc")];
	/**
	 */
	private var m_obApp:ChcPlayerApp = new ChcPlayerApp();
	/**
	 */
	private var m_obMenu:NativeMenu = new NativeMenu();
	/**
	 */
	private var m_miNewFile:NativeMenuItem = new NativeMenuItem("新建(&N)");
	private var m_miOpenFile:NativeMenuItem = new NativeMenuItem("打开(&O)");
	/**
	 */
	private var m_miSaveFile:NativeMenuItem = new NativeMenuItem("保存(&S)");
	private var m_miSaveFileAS:NativeMenuItem = new NativeMenuItem("另存为(&A)");
	/**
	 */
	private var m_miExportBg:NativeMenuItem = new NativeMenuItem("导出背景图(&B)");
	private var m_miExportPic:NativeMenuItem = new NativeMenuItem("导出前景图(&F)");
	/**
	 */
	private var m_miCloseFile:NativeMenuItem = new NativeMenuItem("关闭文件(&C)");
	/**
	 */
	private var m_miExit:NativeMenuItem = new NativeMenuItem("退出(&X)");
	/**
	 * 不用快捷键$避免误操作
	 */
	private var m_miSwitchToEdit:NativeMenuItem = new NativeMenuItem("打开编辑模式");
	/**
	 */
	private var m_miChooseBgPic:NativeMenuItem = new NativeMenuItem("选择背景(&B)");
	/**
	 */
	private var m_miChoosePic:NativeMenuItem = new NativeMenuItem("选择前景(&F)");
	/**
	 */
	private var m_miSetBgColor:NativeMenuItem = new NativeMenuItem("设置背景色(&G)");
	/**
	 */
	private var m_miSetBgRectEx:NativeMenuItem = new NativeMenuItem("设置扩边(&X)");
	/**
	 */
	private var m_miSetScaling:NativeMenuItem = new NativeMenuItem("设置比例(&S)");
	/**
	 * 选择背景色
	 */
	private var m_miChooseBgColor:NativeMenuItem = new NativeMenuItem("选择背景色(&B)");
	/**
	 * 放大
	 */
	private var m_miZoomIn:NativeMenuItem = new NativeMenuItem("放大");
	private var m_miZoomOut:NativeMenuItem = new NativeMenuItem("缩小");
	/**
	 */
	private var m_miFitBG:NativeMenuItem = new NativeMenuItem("适配背景");
	private var m_miFitFG:NativeMenuItem = new NativeMenuItem("适配前景");
	/**
	 * 帮助提示
	 */
	private var m_miTipDrag:NativeMenuItem = new NativeMenuItem("Ctrl左键(或右键)可拖动图片");
	private var m_miAbout:NativeMenuItem = new NativeMenuItem("关于(&A)");
	/**
	 * 关于窗
	 */
	private var m_windowAbout:AboutWindow = new AboutWindow();
	/**
	 * 选择程序背景色$窗口
	 */
	private var m_windowChooseBC:ChooseBgColorWindow = new ChooseBgColorWindow();
	/**
	 */
	private var m_windowSetBgRect:SetBgRectExWindow = new SetBgRectExWindow();
	/**
	 * 设置图形的背景色
	 */
	private var m_windowSetBgColor:SetBgColorWindow = new SetBgColorWindow();
	/**
	 */
	private var m_windowSetScaling:SetScalingWindow = new SetScalingWindow();
	/**
	 */
	public function CHCPlayer()
	{
		if ( stage != null )
		{
			this._init();
		}
		else
		{
			this.addEventListener(Event.ADDED_TO_STAGE, toStage_this);
		}
	}
//--------------------------------------------------------------------------------------------------
// private
//--------------------------------------------------------------------------------------------------
	private function _init():void
	{
		this.stage.scaleMode = StageScaleMode.NO_SCALE;
		this.stage.align = StageAlign.TOP_LEFT;
		m_obApp.setSizeWH(stage.stageWidth, stage.stageHeight);
		this._createMenu();
		/*
		*/
		this.addChild(m_obApp);
		this.stage.nativeWindow.menu = m_obMenu;
		this._updateByModel();
		/*
		*/
		NativeApplication.nativeApplication.addEventListener(InvokeEvent.INVOKE, invoke_app);
		this.stage.addEventListener(Event.RESIZE, resize_stage);
		this.stage.nativeWindow.addEventListener(Event.CLOSE, close_nw);
		m_obApp.addEventListener(Event.CHANGE, change_app);
	}
	/**
	 */
	private function _updateByModel():void
	{
		m_miChooseBgPic.enabled = m_miChoosePic.enabled = m_obApp.getEditMode();
		m_miSetBgRectEx.enabled = m_miSetBgColor.enabled = m_obApp.getEditMode();
		m_miSetScaling.enabled = m_obApp.getEditMode();
		/*
		*/
		m_miSwitchToEdit.checked = m_obApp.getEditMode();
		this._setTitle();
	}
	/**
	 */
	private function change_app(e:Event):void
	{
		this._updateByModel();
	}
	/**
	 */
	private function _createMenu():void
	{
		m_miNewFile.keyEquivalent = "n";
		m_miOpenFile.keyEquivalent = "o";
		m_miSaveFile.keyEquivalent = "s";
		m_miSaveFileAS.keyEquivalent = "s";
		m_miSaveFileAS.keyEquivalentModifiers = [Keyboard.CONTROL, Keyboard.ALTERNATE];
		m_miCloseFile.keyEquivalent = "w";
		/*
		*/
		m_miChoosePic.keyEquivalent = "f";
		m_miSetBgRectEx.keyEquivalentModifiers = [Keyboard.CONTROL, Keyboard.ALTERNATE];
		m_miSetBgRectEx.keyEquivalent = "o";
		m_miSetBgColor.keyEquivalent = "b";
		/**
		 */
		m_miZoomIn.keyEquivalent = "=";
		m_miZoomOut.keyEquivalent = "-";
		m_miFitBG.keyEquivalent = "0";
		m_miFitBG.keyEquivalentModifiers = [Keyboard.CONTROL, Keyboard.ALTERNATE];
		m_miFitFG.keyEquivalent = "0";
		/**
		 */
		var miSP:NativeMenuItem = new NativeMenuItem("", true);
		var miSP2:NativeMenuItem = new NativeMenuItem("", true);
		var miSP3:NativeMenuItem = new NativeMenuItem("", true);
		var arrFile:Array = [m_miNewFile, m_miOpenFile, miSP, m_miSaveFile, m_miSaveFileAS, miSP2, m_miCloseFile, miSP3];
		miSP = new NativeMenuItem("", true);
		arrFile.push(m_miExportBg, m_miExportPic, miSP, m_miExit);
		/*
		*/
		var miFile:NativeMenuItem = this._createMI("文件(&F)", arrFile);
		/*
		*/
		miSP = new NativeMenuItem("", true);
		miSP2 = new NativeMenuItem("", true);
		miSP3 = new NativeMenuItem("", true);
		var arrEdit:Array = [m_miSwitchToEdit, miSP, m_miChooseBgPic, m_miChoosePic, miSP2, m_miSetBgColor, m_miSetBgRectEx, miSP3, m_miSetScaling];
		var miEdit:NativeMenuItem = this._createMI("编辑(&E)", arrEdit);
		/*
		*/
		miSP = new NativeMenuItem("", true);
		var arrView:Array = [m_miZoomIn, m_miZoomOut, miSP, m_miFitBG, m_miFitFG];
		var miView:NativeMenuItem = this._createMI("视图(&V)", arrView);
		/*
		*/
		var arrOption:Array = [m_miChooseBgColor];
		var miOption:NativeMenuItem = this._createMI("选项(&O)", arrOption);
		/*
		*/
		miSP = new NativeMenuItem("", true);
		var miHelp:NativeMenuItem = this._createMI("帮助(&H)", [m_miAbout, miSP, m_miTipDrag]);
		
		m_obMenu.items = [miFile, miEdit, miView, miOption, miHelp];
		/*
		*/
		m_miNewFile.addEventListener(Event.SELECT, select_miNew);
		m_miOpenFile.addEventListener(Event.SELECT, select_miOpen);
		m_miSaveFile.addEventListener(Event.SELECT, select_miSave);
		m_miCloseFile.addEventListener(Event.SELECT, select_miCloseFile);
		m_miExportBg.addEventListener(Event.SELECT, select_miExBg);
		m_miExportPic.addEventListener(Event.SELECT, select_miExPic);
		m_miExit.addEventListener(Event.SELECT, select_Quit);
		/*
		*/
		m_miChooseBgPic.addEventListener(Event.SELECT, select_miBgPic);
		m_miChoosePic.addEventListener(Event.SELECT, select_miPic);
		m_miSetBgColor.addEventListener(Event.SELECT, select_miSetBgColor);
		m_miSetBgRectEx.addEventListener(Event.SELECT, select_miBgRect);
		m_miSetScaling.addEventListener(Event.SELECT, select_miScaling);
		/*
		*/
		m_miSwitchToEdit.addEventListener(Event.SELECT, select_miSwitch);
		m_miChooseBgColor.addEventListener(Event.SELECT, select_chooseBC);
		/*
		*/
		m_miZoomIn.addEventListener(Event.SELECT, select_miZoom);
		m_miZoomOut.addEventListener(Event.SELECT, select_miZoom);
		m_miFitBG.addEventListener(Event.SELECT, select_miZoom);
		m_miFitFG.addEventListener(Event.SELECT, select_miZoom);
		/*
		*/
		m_miAbout.addEventListener(Event.SELECT, select_miAbout);
	}
	/**
	 */
	private function select_miZoom(e:Event):void
	{
		var miTrigger:NativeMenuItem = NativeMenuItem(e.currentTarget);
		if ( miTrigger == m_miZoomIn )
		{
			m_obApp.zoomIn();
		}
		else if ( miTrigger == m_miZoomOut )
		{
			m_obApp.zoomOut();
		}
		else if ( miTrigger == m_miFitBG )
		{
			m_obApp.fitBgScale();
		}
		else if ( miTrigger == m_miFitFG )
		{
			m_obApp.fitFgScale();
		}
	}
	/**
	 */
	private function select_miAbout(e:Event):void
	{
		m_windowAbout.centerTo(this.stage.nativeWindow);
		m_windowAbout.show();
	}
	/**
	 */
	private function select_miBgPic(e:Event):void
	{
		var fi:File = new File();
		fi.addEventListener(Event.SELECT, select_fiBgPic);
		fi.addEventListener(Event.CANCEL, cancel_fiBgPic);
		fi.browseForOpen("选择背景图", ARR_PIC_FILTER);
	}
	/**
	 */
	private function select_miPic(e:Event):void
	{
		var fi:File = new File();
		fi.addEventListener(Event.SELECT, select_fiPic);
		fi.addEventListener(Event.CANCEL, cancel_fiPic);
		fi.browseForOpen("选择前景图", ARR_PIC_FILTER);
	}
	/**
	 */
	private function select_miSetBgColor(e:Event):void
	{
		m_windowSetBgColor.centerTo(this.stage.nativeWindow);
		m_windowSetBgColor.setApp(m_obApp);
		m_windowSetBgColor.show();
	}
	/**
	 */
	private function select_miBgRect(e:Event):void
	{
		m_windowSetBgRect.centerTo(this.stage.nativeWindow);
		m_windowSetBgRect.setApp(m_obApp);
		m_windowSetBgRect.show();
	}
	/**
	 */
	private function select_miScaling(e:Event):void
	{
		m_windowSetScaling.centerTo(this.stage.nativeWindow);
		m_windowSetScaling.setApp(m_obApp);
		m_windowSetScaling.show();
	}
	/**
	 * 选择前景
	 */
	private function select_fiPic(e:Event):void
	{
		cancel_fiPic(e);
		m_obApp.setPic(File(e.target) );
	}
	/**
	 * 取消前景
	 */
	private function cancel_fiPic(e:Event):void
	{
		var fi:File = File(e.currentTarget);
		fi.removeEventListener(Event.SELECT, select_fiPic);
		fi.removeEventListener(Event.CANCEL, cancel_fiPic);
	}
	/**
	 * 选择背景
	 */
	private function select_fiBgPic(e:Event):void
	{
		cancel_fiBgPic(e);
		m_obApp.setBgPic(File(e.target) );
	}
	/**
	 * 取消背景
	 */
	private function cancel_fiBgPic(e:Event):void
	{
		var fi:File = File(e.currentTarget);
		fi.removeEventListener(Event.SELECT, select_fiBgPic);
		fi.removeEventListener(Event.CANCEL, cancel_fiBgPic);
	}
	/**
	 */
	private function select_miSwitch(e:Event):void
	{
		var obMI:NativeMenuItem = NativeMenuItem(e.currentTarget);
		if ( obMI == m_miSwitchToEdit )
		{
			m_obApp.setEditMode(!obMI.checked);
		}
	}
	/**
	 */
	private function select_chooseBC(e:Event):void
	{
		m_windowChooseBC.centerTo(this.stage.nativeWindow);
		m_windowChooseBC.setApp( m_obApp );
		m_windowChooseBC.show();
	}
	/**
	 */
	private function _createMI(strL:String, arrSub:Array):NativeMenuItem
	{
		var obMI:NativeMenuItem = new NativeMenuItem(strL);
		var subMenu:NativeMenu = new NativeMenu();
		obMI.submenu = subMenu;
		subMenu.items = arrSub;
		return obMI;
	}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// events
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
	/**
	 */
	private function select_Quit(e:Event):void
	{
		NativeApplication.nativeApplication.exit();
	}
	/**
	 * 导出背景
	 */
	private function select_miExBg(e:Event):void
	{
		
	}
	/**
	 * 导出前景
	 */
	private function select_miExPic(e:Event):void
	{
		
	}
	/**
	 */
	private function select_miSave(e:Event):void
	{
		m_obApp.saveFile();
	}
	/**
	 */
	private function select_miNew(e:Event):void
	{
		m_obApp.createNewFile();
	}
	/**
	 */
	private function select_miOpen(e:Event):void
	{
		var fi:File = new File();
		fi.addEventListener(Event.SELECT, select_fiOpen);
		fi.addEventListener(Event.CANCEL, cancel_fiOpen);
		fi.browseForOpen("打开文件", ARR_CHC_FILTER);
	}
	/**
	 */
	private function select_fiOpen(e:Event):void
	{
		var fi:File = File(e.currentTarget);
		m_obApp.openFile(fi);
		cancel_fiOpen(e);
	}
	/**
	 */
	private function cancel_fiOpen(e:Event):void
	{
		var fi:File = File(e.currentTarget);
		fi.removeEventListener(Event.SELECT, select_fiOpen);
		fi.removeEventListener(Event.CANCEL, cancel_fiOpen);
	}
	/**
	 */
	private function select_miCloseFile(e:Event):void
	{
		m_obApp.closeFile();
	}
	/**
	 */
	private function close_nw(e:Event):void
	{
		NativeApplication.nativeApplication.exit();
	}
	/**
	 */
	private function invoke_app(e:InvokeEvent):void
	{
		var arrArgs:Array = e.arguments;
		if ( arrArgs.length != 0 )
		{
			m_obApp.openFile(new File(arrArgs[0]) );
		}
	}
	/**
	 */
	private function resize_stage(e:Event):void
	{
		m_obApp.setSizeWH(stage.stageWidth, stage.stageHeight);
	}
	/**
	 */
	private function toStage_this(e:Event):void
	{
		this._init();
	}
	/**
	 */
	private function _setTitle():void
	{
		var strFilePath:String = m_obApp.getFilePath();
		if ( strFilePath.length > 0 )
		{
			if ( m_obApp.getNeedSaveFile() )
			{
				this.stage.nativeWindow.title = APP_NAME + " * " + strFilePath;
			}
			else
			{
				this.stage.nativeWindow.title = APP_NAME + " - " + strFilePath;
			}
		}
		else
		{
			this.stage.nativeWindow.title = APP_NAME;
		}
	}
	/**
	 */
}
}